﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace PhysicsSimulator.SinglePendulum
{
    public partial class EditSinglePendulumPage : PhoneApplicationPage
    {
        public EditSinglePendulumPage()
        {
            InitializeComponent();

            _alpha_max.Text = SinglePendulumSettings.Angle.ToString();
            _g.Text = SinglePendulumSettings.Gravity.ToString();
            _l.Text = SinglePendulumSettings.RopeLengh.ToString();
            _m.Text = SinglePendulumSettings.Weight.ToString();
            _fi.Text = SinglePendulumSettings.Fi.ToString();

            alpha_slider.Value = (SinglePendulumSettings.Angle - 5)/30 * 10;
            m_slider.Value = (SinglePendulumSettings.Weight - 1)/4 *10;
            fi_slider.Value = (SinglePendulumSettings.Fi)/180 * 10;
            l_slider.Value = (SinglePendulumSettings.RopeLengh - 1)/8 * 10;
            g_slider.Value = (SinglePendulumSettings.Gravity - 5);
        }

        private void ApplyClick(object sender, System.EventArgs e)
        {
        	// TODO: Add event handler implementation here.


            if (float.Parse(_alpha_max.Text) > 35 || float.Parse(_alpha_max.Text) < 5)
            {
                MessageBox.Show("Góc dao động Alpha nằm trong khoảng 5-35°");
            }
            else
            {
                if (float.Parse(_m.Text) > 5 || float.Parse(_m.Text) < 1)
                {
                    MessageBox.Show("Khối lượng của vật nằm trong khoảng 1-5Kg");
                }
                else
                {
                    if (float.Parse(_fi.Text) > 180)
                    {
                        MessageBox.Show("Pha ban đầu nằm trong khoảng 0-180°");
                    }
                    else
                    {
                        if (float.Parse(_l.Text) > 9 || float.Parse(_l.Text) < 1)
                        {
                            MessageBox.Show("Chiều dài dây treo nằm trong khoảng 1-9m");
                        }
                        else
                        {
                            if (float.Parse(_g.Text) > 15 || float.Parse(_g.Text) < 5)
                            {
                                MessageBox.Show("Gia tốc trọng trường nằm trong khoảng 5-15m/s²");
                            }
                            else
                            {
                                SinglePendulumSettings.Angle = float.Parse(_alpha_max.Text);
                                SinglePendulumSettings.Gravity = float.Parse(_g.Text);
                                SinglePendulumSettings.RopeLengh = float.Parse(_l.Text);
                                SinglePendulumSettings.Weight = float.Parse(_m.Text);
                                SinglePendulumSettings.Fi = float.Parse(_fi.Text);

                                SinglePendulumSettings.check_apply = true;

                                NavigationService.GoBack();
                            }
                        }
                    }
                }
            }
        }

        private void alpha_slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            float value = 5 + 30 * (float)alpha_slider.Value / 10;
            _alpha_max.Text = value.ToString();
        }

        private void m_slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            float value = 1 + 4 * (float)m_slider.Value / 10;
            _m.Text = value.ToString();
        }

        private void fi_slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            float value = 180 * (float)fi_slider.Value / 10;
            _fi.Text = value.ToString();
        }

        private void l_slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            float value = 1 + 8 * (float)l_slider.Value / 10;
            _l.Text = value.ToString();
        }

        private void g_slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            float value = 5 + (float)g_slider.Value;
            _g.Text = value.ToString();
        }

        private void _alpha_max_LostFocus(object sender, RoutedEventArgs e)
        {
            alpha_slider.Value = (float.Parse(_alpha_max.Text) - 5) / 30 * 10;
            
        }

        private void _m_LostFocus(object sender, RoutedEventArgs e)
        {
            m_slider.Value = (float.Parse(_m.Text) - 1) / 4 * 10;
            
        }

        private void _fi_LostFocus(object sender, RoutedEventArgs e)
        {
            fi_slider.Value = (float.Parse(_fi.Text)) / 180 * 10;
            
        }

        private void _l_LostFocus(object sender, RoutedEventArgs e)
        {
            l_slider.Value = (float.Parse(_l.Text) - 1) / 8 * 10;
            
        }

        private void _g_LostFocus(object sender, RoutedEventArgs e)
        {
            g_slider.Value = (float.Parse(_g.Text) - 5);
        }
    }
}